package com.jinkaimen.goodcash.ui.weiget.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jinkaimen.goodcash.R;


/**
 * 自定义popwindow，主要用来显示在底部
 * <p>
 */

public class CustomPopwindow extends PopupWindow {

    private ViewHolder viewHolder;
    private boolean isAutoDismiss = true;
    private Context context;
    private final Animation animationUp;
    private final Animation animationDown;

    public CustomPopwindow(Context context) {
        super(context);
        this.context = context;
        viewHolder = new ViewHolder(View.inflate(context, R.layout.layout_pop, null));
        setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.pop_bg_gray)));
        setContentView(viewHolder.rootView);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setOutsideTouchable(true);
        setFocusable(true);
        animationUp = AnimationUtils.loadAnimation(context, R.anim.translate_up);
        animationDown = AnimationUtils.loadAnimation(context, R.anim.translate_down);

//        setAnimationStyle(R.style.PopupAnimationUpDown);
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissPop();
            }
        };
        viewHolder.tv_cancle.setOnClickListener(clickListener);
        viewHolder.rootView.setOnClickListener(clickListener);
        update();
    }


    @Override
    public void setContentView(View contentView) {
        super.setContentView(contentView);
    }

    /**
     * 设置title
     *
     * @param title
     * @return
     */
    public CustomPopwindow setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            viewHolder.tv_title.setText(title);
            viewHolder.tv_title.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tv_title.setVisibility(View.GONE);
        }
        return this;
    }

    /**
     * 设置取消按钮
     *
     * @param cancle
     * @return
     */
    public CustomPopwindow setCancle(String cancle) {
        if (!TextUtils.isEmpty(cancle)) {
            viewHolder.tv_cancle.setText(cancle);
            viewHolder.tv_cancle.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tv_cancle.setVisibility(View.GONE);
        }
        return this;
    }

    /**
     * 显示在底部
     *
     * @param parent
     */
    public void showAtBottom(View parent) {
        viewHolder.rootView.startAnimation(animationUp);
        showAtLocation(parent, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 0);
    }

    /**
     * 显示在顶部
     *
     * @param parent
     */
    public void showAtTop(View parent) {
        viewHolder.rootView.startAnimation(animationUp);
        showAtLocation(parent, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 0);
    }


    /**
     * 设置条目监听
     *
     * @param tvBtnClickListener
     */
    public void setTvBtnClickListener(OnBtnClickListener[] tvBtnClickListener) {
        try {
            viewHolder.divier.setVisibility(tvBtnClickListener.length == 0 ? View.GONE : View.VISIBLE);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
            for (int i = 0; i < tvBtnClickListener.length; i++) {
                if (i > 0) {
                    View divider = View.inflate(context, R.layout.layout_divider, null);
                    divider.setLayoutParams(params);
                    viewHolder.ll_btn_content.addView(divider);
                }
                final OnBtnClickListener clickListener = tvBtnClickListener[i];
                final TextView dialogTvBtn = CustomButtons.getPopTvItem(context, clickListener.getTvBtnText(), clickListener.textColor);
                dialogTvBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isAutoDismiss && isShowing()) {
                            dismissPop();
                        }
                        clickListener.onClick(CustomPopwindow.this, dialogTvBtn);
                    }
                });
                viewHolder.ll_btn_content.addView(dialogTvBtn);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 同时添加多个item，使用同一个监听回调
     *
     * @param items              每个item的文本内容
     * @param colors             为null时每个item都是默认颜色，如果不为空，那么colors的长度必须和items长度一致，一一对应
     * @param onBtnClickListener 监听器,在view中可通过获取tag来获取当前点击的条目position
     */
    public void setBtnsClickListener(String[] items, int[] colors, final OnBtnClickListener onBtnClickListener) {
        try {
            viewHolder.divier.setVisibility(items.length == 0 ? View.GONE : View.VISIBLE);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
            for (int i = 0; i < items.length; i++) {
                if (i > 0) {
                    View divider = View.inflate(context, R.layout.layout_divider, null);
                    divider.setLayoutParams(params);
                    viewHolder.ll_btn_content.addView(divider);
                }
                final TextView dialogTvBtn = CustomButtons.getPopTvItem(context, items[i], colors == null ? -1 : (colors.length > i ? colors[i] : -1));
                dialogTvBtn.setTag(i);
                dialogTvBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isAutoDismiss && isShowing()) {
                            dismissPop();
                        }
                        onBtnClickListener.onClick(CustomPopwindow.this, dialogTvBtn);
                    }
                });
                viewHolder.ll_btn_content.addView(dialogTvBtn);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
    }

    public static abstract class OnBtnClickListener {
        private String tvBtnText;
        private int textColor;

        public OnBtnClickListener() {
        }

        public OnBtnClickListener(String tvBtnText) {
            this(tvBtnText, -1);
        }

        /**
         * 点击回调
         *
         * @param tvBtnText 文本
         * @param textColor 颜色值是以dialog开头的
         */
        public OnBtnClickListener(String tvBtnText, int textColor) {
            this.tvBtnText = tvBtnText;
            this.textColor = textColor;
        }

        public String getTvBtnText() {
            return tvBtnText;
        }

        public abstract void onClick(CustomPopwindow popwindow, View view);
    }

    public boolean isAutoDismiss() {
        return isAutoDismiss;
    }

    /**
     * 设置是否点击后自动消失
     *
     * @return
     */
    public CustomPopwindow setAutoDismiss(boolean autoDismiss) {
        isAutoDismiss = autoDismiss;
        return this;
    }


    private  class ViewHolder {
        public View rootView;
        public TextView tv_title;
        public TextView tv_cancle;
        public View divier;
        public LinearLayout ll_btn_content;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_title = (TextView) rootView.findViewById(R.id.tv_title);
            this.tv_cancle = (TextView) rootView.findViewById(R.id.tv_cancle);
            this.divier = rootView.findViewById(R.id.divider);
            this.ll_btn_content = (LinearLayout) rootView.findViewById(R.id.ll_btn_content);
        }
    }

    public ViewHolder getViewHolder() {
        return viewHolder;
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    /**
     * 隐藏pop
     */
    public void dismissPop() {
        animationDown.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        viewHolder.rootView.startAnimation(animationDown);
    }
}
